package com.apestech.framework.sql.lib;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.table.modul.Tables;
import java.util.ArrayList;
import java.util.UUID;

public class TableOper {

    private Tables table;
    private Table tab;
    private String pk;
    private JSONObject index;
    private SqlSession session;
    private String stableName;
    private String dtableName;

    public TableOper(SqlSession session, String stableName, String dtableName) throws Exception {
        this.stableName=stableName.toUpperCase();
        this.dtableName=dtableName.toUpperCase();
        this.session=session;
        table=new Tables();
        JSONObject m=new JSONObject();
        m.put("tabName", dtableName);
        m.put("tabComment", dtableName);
        table.addTabInfo(dtableName, m);
        table.addMasterTab(dtableName, dtableName, "1","1");
        tab=session.getDictTable(stableName);
        pk="";
        if(tab.getPkKey()!=null){
            pk=tab.getPkKey().getPkColumn();
        }
    }

    public Tables getTableFromTable() throws Exception {
        ArrayList<String> klist=tab.getField().getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String k=klist.get(i);
            table.addField(dtableName, k, (";"+pk+";").indexOf(";"+k+";")>=0, (Table.Field) tab.getField().get(k));
        }
        index=session.getForDDL().getIndexInfo(stableName);
        klist=index.getKeylist();
        if(index!=null){
            len=klist.size();
            for(int i=0; i<len; i++){
                String k=klist.get(i);
                JSONObject inx=new JSONObject();
                inx.put("sufname", k.substring(k.lastIndexOf("_")+1, k.length()));
                inx.put("fields", index.get(k));
                table.addUniqueIndex(dtableName, inx);
            }
        }
        return table;
    }

    public Tables getImportTableFromTable() throws Exception {
        JSONObject f=new JSONObject();
        f.put("id","IMP_ID");
        f.put("isKey","true");
        f.put("notNull","true");
        f.put("type","VARCHAR2(32)");
        f.put("comment","导入编码");
        table.addField(dtableName, f);
        f=new JSONObject();
        f.put("id","IMP_DATE");
        f.put("type","TIMESTAMP(6)");
        f.put("comment","导入时间");
        table.addField(dtableName, f);
        f=new JSONObject();
        f.put("id","IMP_XLS_ID");
        f.put("type","VARCHAR2(32)");
        f.put("isKey","true");
        f.put("notNull","true");
        f.put("comment","XLS行号");
        table.addField(dtableName, f);
        f=new JSONObject();
        f.put("id","IMP_STATE");
        f.put("type","VARCHAR2(255)");
        f.put("defaultValue","0");
        f.put("comment","导入状态(0:成功)");
        table.addField(dtableName, f);
        f=new JSONObject();
        f.put("id","IMP_MSG");
        f.put("type","CLOB");
        f.put("comment","错误信息");
        table.addField(dtableName, f);
        ArrayList<String> klist=tab.getField().getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String k=klist.get(i);
            if(k.equals("IMP_ID") || k.equals("IMP_DATE") || k.equals("IMP_XLS_ID") || k.equals("IMP_STATE") || k.equals("IMP_MSG")){
                continue;
            }
            Table.Field f1=(Table.Field) tab.getField().get(k);
            f1.setNotNull("false");
            table.addField(dtableName, k, false, f1);
        }
        index=session.getForDDL().getIndexInfo(stableName);
        if(index!=null){
            klist=index.getKeylist();
            len=klist.size();
            for(int i=0; i<len; i++){
                String k=klist.get(i);
                JSONObject inx=new JSONObject();
                String sufname=k.substring(k.lastIndexOf("_")+1, k.length());
                inx.put("sufname", sufname);
                inx.put("fields", index.get(k)+",IMP_ID,IMP_STATE");
                table.addUniqueIndex(dtableName, inx);
            }
        }
        return table;
    }

    public Tables getStopTableFromTable() throws Exception {
        JSONObject f=new JSONObject();
        f.put("id","STOP_ID");
        f.put("isKey","true");
        f.put("notNull","true");
        f.put("type","VARCHAR2(32)");
        f.put("comment","终止编码");
        table.addField(dtableName, f);
        ArrayList<String> klist=tab.getField().getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String k=klist.get(i);
            if(k.equals("STOP_ID") ){
                continue;
            }
            Table.Field f1=(Table.Field) tab.getField().get(k);
            f1.setNotNull("false");
            table.addField(dtableName, k, false, f1);
        }
        index=session.getForDDL().getIndexInfo(stableName);
        if(index!=null){
            klist=index.getKeylist();
            len=klist.size();
            for(int i=0; i<len; i++){
                String k=klist.get(i);
                table.getTable(dtableName).setPrimaryField(index.get(k)+",STOP_ID");
                /*JSONObject inx=new JSONObject();
                inx.put("sufname", k.substring(k.lastIndexOf("_")+1, k.length()));
                inx.put("fields", index.get(k)+",STOP_ID");
                table.addUniqueIndex(dtableName, inx);
                */
            }
        }
        return table;
    }

}
